package com.edie.customview.basemvp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.edie.customview.R;

/**
 * edie create on 2018/7/19
 */
public abstract class BaseMvpActivity<P extends IPresenter> extends BaseActivity implements IView {

    protected P mPresenter;
    private ViewGroup mRootView;
    private View mView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = initPresenter();
    }

    protected abstract P initPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDetach();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError() {
        mRootView = (ViewGroup) getWindow().getDecorView();
        mView = LayoutInflater.from(this)
                .inflate(R.layout.layout_error, mRootView, false);
        mRootView.addView(mView);

    }

    public void hideError() {
        if (mRootView != null && mView != null) {
            mRootView.removeView(mView);
        }
    }
}
